package guda.house.web.action.sys.village;

import guda.grape.autogen.common.page.BizResult;
import guda.grape.util.RequestUtil;
import guda.house.biz.*;
import guda.house.biz.beans.vo.JsonResult;
import guda.house.biz.constants.CommonResultCode;
import guda.house.biz.enums.OwnerTypeEnum;
import guda.house.biz.query.*;
import guda.house.biz.util.ErrorCode;
import guda.house.biz.util.PriceUtil;
import guda.house.dal.dao.DepositRuleDAO;
import guda.house.dal.dao.ExtRuleDAO;
import guda.house.dal.dao.RenderDAO;
import guda.house.dal.dataobject.DepositRuleDO;
import guda.house.dal.dataobject.ExtRuleDO;
import guda.house.dal.dataobject.OwnerDO;
import guda.house.dal.dataobject.RenderDO;
import guda.house.web.helper.ResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by well on 15/11/14.
 */
@Controller
public class VillageCommonAction {

    @Autowired
    private VillageBiz villageBiz;
    @Autowired
    private BuildingBiz buildingBiz;
    @Autowired
    private UnitBiz unitBiz;
    @Autowired
    private SuiteBiz suiteBiz;
    @Autowired
    private OwnerBiz ownerBiz;
    @Autowired
    private DepositRuleDAO depositRuleDAO;
    @Autowired
    private ExtRuleDAO extRuleDAO;
    @Autowired
    private RenderDAO renderDAO;

    @RequestMapping(value = "sys/common/getBuilding.htm")
    public void getBuilding(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        long villageId = RequestUtil.getLong(request, "villageId");
        JsonResult result = JsonResult.getInstance();
        if (villageId == 0) {
            result.setErrMsg(ErrorCode.getMessage(CommonResultCode.PARAM_MISS));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }
        BuildingQuery buildingQuery = new BuildingQuery();
        buildingQuery.setPageSize(10000);
        buildingQuery.setVillageId(villageId);

        modelMap.put("query", buildingQuery);
        BizResult bizResult = buildingBiz.list(buildingQuery);
        if (bizResult.success) {
            result.getMap().putAll(bizResult.data);
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        } else {
            result.setErrMsg(ErrorCode.getMessage(bizResult.code));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }
    }

    @RequestMapping(value = "sys/common/getUnit.htm")
    public void getUnit(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        long buildingId = RequestUtil.getLong(request, "buildingId");
        JsonResult result = JsonResult.getInstance();
        if (buildingId == 0) {
            result.setErrMsg(ErrorCode.getMessage(CommonResultCode.PARAM_MISS));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }
        UnitQuery unitQuery = new UnitQuery();
        unitQuery.setBuildingId(buildingId);

        modelMap.put("query", unitQuery);
        BizResult bizResult = unitBiz.listForCommon(unitQuery);
        if (bizResult.success) {
            result.getMap().putAll(bizResult.data);
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        } else {
            result.setErrMsg(ErrorCode.getMessage(bizResult.code));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }
    }

    @RequestMapping(value = "sys/common/getSuite.htm")
    public void getSuite(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        long unitId = RequestUtil.getLong(request, "unitId");
        JsonResult result = JsonResult.getInstance();
        if (unitId == 0) {
            result.setErrMsg(ErrorCode.getMessage(CommonResultCode.PARAM_MISS));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }
        SuiteQuery suiteQuery = new SuiteQuery();
        suiteQuery.setUnitId(unitId);

        modelMap.put("query", suiteQuery);
        BizResult bizResult = suiteBiz.listForCommon(suiteQuery);
        if (bizResult.success) {
            result.getMap().putAll(bizResult.data);
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        } else {
            result.setErrMsg(ErrorCode.getMessage(bizResult.code));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }
    }

    @RequestMapping(value = "sys/common/getOwner.htm")
    public void getOwner(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        long suiteId = RequestUtil.getLong(request, "suiteId");
        String ownerType = request.getParameter("ownerType");
        JsonResult result = JsonResult.getInstance();
        if (suiteId == 0) {
            result.setErrMsg(ErrorCode.getMessage(CommonResultCode.PARAM_MISS));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }
        if(OwnerTypeEnum.rent.name().equals(ownerType)){
            RenderQuery renderQuery = new RenderQuery();
            renderQuery.setSuiteId(suiteId);
            List<RenderDO> renderDOs = renderDAO.selectForPage(renderQuery);
            List<OwnerDO> ownerList = new ArrayList<OwnerDO>();
            for(RenderDO renderDO:renderDOs){
                OwnerDO ownerDO = new OwnerDO();
                ownerDO.setOwnerName(renderDO.getRenderName());
                ownerDO.setOwnerId(renderDO.getRenderId());
                ownerList.add(ownerDO);
            }
            result.getMap().put("ownerList",ownerList);
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }else {
            OwnerQuery ownerQuery = new OwnerQuery();
            ownerQuery.setSuiteId(suiteId);

            modelMap.put("query", ownerQuery);
            BizResult bizResult = ownerBiz.list(ownerQuery);
            if (bizResult.success) {
                result.getMap().putAll(bizResult.data);
                ResponseHelper.ajaxReturn(result.getMap(), response);
                return;
            } else {
                result.setErrMsg(ErrorCode.getMessage(bizResult.code));
                ResponseHelper.ajaxReturn(result.getMap(), response);
                return;
            }
        }
    }

    @RequestMapping(value = "sys/common/getDepositVal.htm")
    public void getBankCharge(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        long villageId = RequestUtil.getLong(request, "villageId");
        long varId = RequestUtil.getLong(request, "varId");
        JsonResult result = JsonResult.getInstance();


        DepositRuleQuery depositRuleQuery = new DepositRuleQuery();
        depositRuleQuery.setVillageId(villageId);
        depositRuleQuery.setVarId(varId);
        List<DepositRuleDO> depositRuleDOList = depositRuleDAO.selectForPage(depositRuleQuery);
        if(depositRuleDOList.size()>0) {
            result.getMap().put("fee", PriceUtil.formatCNY(depositRuleDOList.get(0).getFee()));
        }
        ResponseHelper.ajaxReturn(result.getMap(), response);
        return;


    }

    @RequestMapping(value = "sys/common/getOtherVal.htm")
    public void getOtherVal(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        long villageId = RequestUtil.getLong(request, "villageId");
        long varId = RequestUtil.getLong(request, "varId");
        JsonResult result = JsonResult.getInstance();


        ExtRuleQuery extRuleQuery = new ExtRuleQuery();
        extRuleQuery.setVillageId(villageId);
        extRuleQuery.setVarId(varId);
        List<ExtRuleDO> extRuleDOList = extRuleDAO.selectForPage(extRuleQuery);
        if(extRuleDOList.size()>0) {
            result.getMap().put("fee", PriceUtil.formatCNY(extRuleDOList.get(0).getFee()));
        }
        ResponseHelper.ajaxReturn(result.getMap(), response);
        return;


    }

}
